<script setup lang="ts">
import { useRoute } from 'vue-router'
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import { Sku } from '@/types/goods'
import { ref } from 'vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
const route = useRoute()
const { goodsStore } = useStore()
const { goods } = storeToRefs(goodsStore)
const onGetSku = (sku: Sku) => {
  // console.log(sku)
  goods.value.price = sku.price
  goods.value.oldPrice = sku.oldPrice
}

goodsStore.getGoodsInfo(route.params.goodsId as string)

// 选择购买数量
const count = ref(1)

const addShopCar = () => {
  console.log('加入购物车成功')
}
</script>
<template>
  <div class="goods-container">
    <div class="container" v-if="goods.name">
      <!-- 面包屑 -->
      <xtx-bread>
        <xtx-bread-item>首页</xtx-bread-item>
        <xtx-bread-item>{{ goods.categories[1].name }}</xtx-bread-item>
        <xtx-bread-item>{{ goods.categories[0].name }}</xtx-bread-item>
        <xtx-bread-item>{{ goods.name }}</xtx-bread-item>
      </xtx-bread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :goodsImg="goods.mainPictures"></GoodsImage>
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods" />
          <GoodsSku
            @getSku="onGetSku"
            :goods="goods"
            skuId="1369155864430120962"
          />
          <!-- :count="count" -->
          <!-- @update:count="count = $event" -->
          <XtxNumbox v-model:count="count" :isShowLabel="true" />
          <!-- vue3中 原生事件 默认作用于组件的根节点-->
          <!-- vue2中 原生事件 默认无效 -->
          <!-- 1. @click.native -->
          <!-- 2. 组件内部通过emit去发 -->
          <xtx-button
            @click="addShopCar"
            style="margin: 10px 5px"
            size="middle"
            type="primary"
          >
            加入购物车
          </xtx-button>
        </div>
      </div>

      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail :goods="goods" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
    <div v-else>加载中...</div>
  </div>
</template>
<style lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
</style>
